﻿using PSDLib.Download;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.DeviceTree
{
    public partial class DeviceTreeDaHua : UserControl, IDeviceTree
    {
        List<dl_server_config> serverConfig = new List<dl_server_config>();
        List<PSDLib.Download.ChannelInfo> channelList = new List<PSDLib.Download.ChannelInfo>();
        List<PSDLib.Download.ChannelInfo> searchResult = null;
        int currSearchResultIndex = 0;
        public DeviceTreeDaHua()
        {
            InitializeComponent();
        }

        public PSDLib.Download.ChannelInfo ChannelInfo { get; set; }

        public event OnCameraSelectedEventHanlder CameraSelected;
        public event EventHandler Closed;

        public void Init(List<dl_server_config> serverList)
        {
            serverConfig.AddRange(serverList.Where(t => t.storage_server_type == DownloadServerType.DaHua.ToString()).ToList());

            foreach (var server in serverConfig)
            {
                IDownloadServer dServer = DownloadServerFactory.Create(server);

                if (dServer.Init())
                {
                    if (dServer.Login())
                    {
                        TreeNode node_server = new TreeNode();
                        node_server.Text = server.storage_server_name;
                        node_server.Name = server.id;
                        node_server.Tag = server;

                        tvwAllChannel.Nodes.Add(node_server);

                        var channels = (dServer as DaHuaDownloadServer).GetChannelList();

                        channelList.AddRange(channels);

                        foreach (var ch in channels)
                        {
                            TreeNode node_ch = new TreeNode();
                            node_ch.Text = "通道 - " + ch.Name;
                            node_ch.Name = server.id + "_" + ch.Id;
                            node_ch.Tag = ch;

                            node_server.Nodes.Add(node_ch);
                        }
                    }

                    dServer.Logout();
                }

                dServer.Cleanup();

            }

            tvwAllChannel.ExpandAll();

            if (tvwAllChannel.Nodes.Count > 0)
                tvwAllChannel.SelectedNode = tvwAllChannel.Nodes[0];
        }

        public void Accept()
        {
            if (tvwAllChannel.SelectedNode != null && tvwAllChannel.SelectedNode.Level == 1)
            {
                if (CameraSelected != null)
                {
                    CameraSelected((tvwAllChannel.SelectedNode.Tag as PSDLib.Download.ChannelInfo), (tvwAllChannel.SelectedNode.Parent.Tag as dl_server_config));
                }
            }
            else
            {
                MessageBox.Show("请选择一个摄像机通道");
            }
        }

        public void Cancel()
        {
            if (Closed != null)
            {
                Closed(this, null);
            }
        }

        private void tvwAllChannel_DoubleClick(object sender, EventArgs e)
        {
            Accept();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                if (searchResult == null)
                {
                    searchResult = channelList.Where(t => t.Name.Contains(txtSearch.Text.Trim())).ToList();
                    currSearchResultIndex = 0;

                    SelectSearchResultNode(searchResult[currSearchResultIndex]);

                    if (searchResult.Count > 1)
                    {
                        btnSearch.Text = "查找下一个";
                    }
                    else
                    {
                        btnSearch.Text = "查找";
                    }
                }
                else
                {
                    if (currSearchResultIndex == searchResult.Count - 1)
                    {
                        if (MessageBox.Show("已无更多匹配项，是否从头开始搜索？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            currSearchResultIndex = 0;
                            SelectSearchResultNode(searchResult[currSearchResultIndex]);
                        }
                    }
                    else
                    {
                        currSearchResultIndex += 1;
                        SelectSearchResultNode(searchResult[currSearchResultIndex]);
                    }
                }
            }
        }

        private void SelectSearchResultNode(PSDLib.Download.ChannelInfo channel)
        {
            var n = tvwAllChannel.Nodes.Find(channel.ServerId + "_" + channel.Id, true);

            if (n.Length > 0)
            {
                tvwAllChannel.SelectedNode = n[0];
                tvwAllChannel.Select();
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Length == 0)
            {
                searchResult = null;
                currSearchResultIndex = 0;
                btnSearch.Text = "查找";
            }
        }
    }
}
